package cn.lanchengkeji.datafillingsystem.controller;



import cn.lanchengkeji.datafillingsystem.utils.SCaptcha;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
@RequestMapping(value = "/verification")
public class VerificationController {

    @ResponseBody
    @RequestMapping(value = "/verificationImg", method = RequestMethod.POST)
    public ResponseEntity<byte[]> verification(HttpServletResponse response) throws IOException {
        // 设置响应的类型格式为图片格式
        response.setContentType("image/jpeg");
        // 禁止图像缓存
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        //实例生成验证码对象
        SCaptcha instance = new SCaptcha();
        byte[] imageContent;
        imageContent = instance.write();

        final HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.IMAGE_PNG);
        headers.set("code",instance.getCode());
        headers.set("responseType","arraybuffer");
        /*verificationVO verificationVO = new verificationVO();
        verificationVO.setImageContent(imageContent);
        verificationVO.setCode(instance.getCode());*/

        return new ResponseEntity<>(imageContent, headers, HttpStatus.OK);
    }
}
